package com.tledu.controller;

import com.tledu.model.AjaxResult;
import com.tledu.model.Department;
import com.tledu.model.YongHu;
import com.tledu.service.YongHuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/yonghu")
public class YongHuController {
    @Autowired
    private YongHuService yongHuService;
    @RequestMapping("/yonghu_list")
    //展示页面
    public String YongHu_list(Model model,String cpage){
//        List<YongHu> yongHuList = yongHuService.YongHu_list();
//        model.addAttribute("yongHuList",yongHuList);

        if(cpage==null){
            cpage="1";
        }
        //分页单位
        int pageSize = 3;
        Map<String,Object> map = new HashMap<>();
        Integer start = (Integer.parseInt(cpage)-1);
        map.put("pageSize",pageSize);
        map.put("start",start);
        List<YongHu> yongHuList = yongHuService.YongHu_list(map);
        model.addAttribute("yongHuList",yongHuList);
        //总条数
        int totalCount = yongHuService.YongHuCount();
        //总页数
        int totalPage = totalCount/pageSize + (totalCount % pageSize == 0?0:1);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("cpage",cpage);
        return "yonghu_list";
    }
    @RequestMapping("/deleteAll")
    public String DeleteAll(String[] ids){
        for (String id : ids){
            yongHuService.DeleteAll(Integer.valueOf(id));
        }
        return null;
    }
}
